<template>
    <a-tree-select v-if="treeCheckable || multiple"
        :value="arrayValue"
        :dropdown-style="{maxHeight: maxHeight+'px', overflow: 'auto' }"
        :treeData="dictOptions"
        :treeDefaultExpandAll="treeDefaultExpandAll"
        :disabled="disabled"
        :treeCheckable="treeCheckable"
        :replaceFields="replaceFields"
        :multiple="multiple"
        :showCheckedStrategy="showCheckedStrategy"
        :allow-clear="true"
        @change="handleChange"
        >
    </a-tree-select>
    <a-tree-select v-else
        :value="value"
        :dropdown-style="{maxHeight: maxHeight+'px', overflow: 'auto' }"
        :treeData="dictOptions"
        :treeDefaultExpandAll="treeDefaultExpandAll"
        :disabled="disabled"
        :treeCheckable="treeCheckable"
        :replaceFields="replaceFields"
        :multiple="multiple"
        :showCheckedStrategy="showCheckedStrategy"
        :allow-clear="true"
        @change="handleChange"
        >
    </a-tree-select>
    
</template>

<script>

import { initDict } from '@/api/dict'

export default {
    name:'LTreeSelect',
    props:{
        dictCode:String,
        value:{
            type:String|Number|Array
        },
        treeDefaultExpandAll:{
            type:Boolean,
            default:false
        },
        treeCheckable:{
            type:Boolean,
            default:false
        },
        maxHeight:{
            type:Number,
            default:400
        },
        disabled:{
            type:Boolean,
            default:false
        },
        replaceFields:{
            type:Object,
            default:()=>{}
        },
        showCheckedStrategy:{
            type:String,
            default:'SHOW_ALL'
        },
        dictParam:{
            type:String,
            default:()=>""
        },
        treeCheckStrictly:{
            type:Boolean,
            default:false
        },
        multiple:{
            type:Boolean,
            default:false
        }
          
    },
    created(){
        //初始化下拉数据
        this.initDictData()
    },
    data(){
        return{
            dictOptions:[],
            resultOptions:[],
            arrayValue:null,

        }
    },
    watch:{
        value (val) {
           
            if(this.treeCheckable || this.multiple){
                if(!val){
                    this.arrayValue = []
                }else{
                    this.arrayValue = this.value.split(",")
                }
            }
            
        },
        dictParam(){
            this.initDictData()
            
        }
    },
    methods: {
        handleChange(e) {
         
            if(this.treeCheckable || this.multiple){
                this.$emit('change', e.join(","))
            }else{
                this.$emit('change', e)
            }
            
        },
        initDictData(){
            let that = this
            initDict(that.dictCode,that.dictParam).then(()=>{
                if(that.$store.state.dict.dictInfo[that.dictCode]){
                    that.resultOptions = that.$store.state.dict.dictInfo[that.dictCode].dictInfo
                    that.dictOptions = Object.assign([],that.resultOptions)
                }
                
            })
        },
    }
}
</script>